<?php

/*
 * /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
* php_Util_DB_PDO library
*
* This library provides some classes & methods that provides extra facilities to php DB management
*
* - php_Util_DB_EDITOR_PDO (Provides basic PHP database CRUD facilities for PDO connection type)
*
*
* since 2013-02-21
* version 0.03b
*
*
*
* author: Felipe (felipe.camacho@gmail.com)
* 
*
* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
*/


namespace php_Util\DB;

/**
 * Namespace for Database library
 *
 */
use php_Util as ns_php_Util;


require_once ('php_Util_DB_lib.php');


/**
 * Provides basic PHP database CRUD facilities for PDO connection type
 * @see php_Util_DB_EDITOR
 *
 *
 * @author Felipe
 * 
 */
class php_Util_DB_EDITOR_PDO extends php_Util_DB_EDITOR {
	
	/**
	 * @see php_Util_DB_EDITOR::connect_To_DB()
	 */
	public function connect_To_DB($connection = null) {

		// Set connection if is passed as parameter ---------------------------|\/|---
		if (is_a($connection, 'php_Util\DB\php_Util_DB_CONNECTION')) {
			$this->db_Connection = $connection;
		}
		// --------------------------------------------------------------------|/\|---
		
		
		// Initialize connection ----------------------------------------------------------------------------------------------------------------------|\/|---
		if ($this->db_Connection->_connection_Link == null) {
				
			$dsn = php_Util_DB::get_DNS('mysql', $this->db_Connection->hostName, $this->db_Connection->default_DB);
			$options = $this->db_Connection->options;
				
			try {
				$this->db_Connection->_connection_Link = new \PDO($dsn, $this->db_Connection->userName, $this->db_Connection->password, $options);
			} catch (\PDOException $e) {
				throw new \Exception("Connection failed. ". $e->getMessage());
			}

		}
		// --------------------------------------------------------------------------------------------------------------------------------------------|/\|---
		
	}


	/**
	 * @see php_Util_DB_EDITOR::create_Object()
	 */
	public function create_Object($tablename, $insertFields) {


		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}


		if (!is_array($insertFields) || sizeof($insertFields) == 0) {
			throw new \Exception("Parameter insertFields required.");
		}
		// --------------------------------------------------------------------|/\|---

			
		// Prepare SQL query --------------------------------------------------------|\/|---
		$sql_Query = null;
		try {
			$sql_Query = self::get_SQL__Insert($tablename, $insertFields);
		} catch (Exception $e) {
			throw new \Exception("Error in SQL query. ". $e->getMessage());
		}
		// --------------------------------------------------------------------------|/\|---


		// Execute query -----------------------------------------------|\/|---
		$db_Connection = $this->db_Connection->_connection_Link;
		$affected_Rows = $db_Connection->exec($sql_Query);
		// -------------------------------------------------------------|/\|---

		return $affected_Rows;

	}


	/**
	 * @see php_Util_DB_EDITOR::read_Object()
	 */
	public function read_Object($tablename, $conditions, $fields) {


		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}

		if ($conditions == null || $conditions == "") {
			throw new \Exception("Parameter conditions required.");
		}
		// --------------------------------------------------------------------|/\|---


		// Prepare SQL query --------------------------------------------------------|\/|---
		$sql_Query = null;
		try {
			$sql_Query = self::get_SQL__Select($tablename, $fields, $conditions);
		} catch (\Exception $e) {
			throw new \Exception( "Error in SQL query. "
					."['Tablename'] = ". print_r($tablename, true) . ", "
					."['fields'] = ". print_r($fields, true) . ", "
					."['conditions'] = ". print_r($conditions, true)
					. ". ". $e->getMessage() );
		}
		// --------------------------------------------------------------------------|/\|---

		
		// Execute query -----------------------------------------------|\/|---
		$db_Connection = $this->db_Connection->_connection_Link;
		
		$statement = $db_Connection->prepare($sql_Query);
		$query_Result = $statement->execute();
		
		if ($query_Result === null || $query_Result === false) {
			throw new \Exception("Bad SQL query result.");
		}
		// -------------------------------------------------------------|/\|---
		
		
		// Prepare result ----------------------------------------------|\/|---
		$result = array();

		while ($result_Row = $statement->fetch(\PDO::FETCH_ASSOC)) {
			array_push($result, $result_Row);
		}
		// -------------------------------------------------------------|/\|---

		
		return $result;

	}



	/**
	 * @see php_Util_DB_EDITOR::update_Object()
	 */
	public function update_Object($tablename, $updateFields, $conditions = null) {
		

		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		if ($updateFields == null || $updateFields == "") {
			throw new \Exception("Parameter updateFields required.");
		}
		// --------------------------------------------------------------------|/\|---
		

		// Prepare SQL query --------------------------------------------------------|\/|---
		$sql_Query = null;
		try {
			$sql_Query = self::get_SQL__Update($tablename, $updateFields, $conditions);
		} catch (\Exception $e) {
			throw new \Exception("Error in SQL query. ". $e->getMessage());
		}
		// --------------------------------------------------------------------------|/\|---
		

		// Execute query -----------------------------------------------|\/|---
		$db_Connection = $this->db_Connection->_connection_Link;
		
		$statement = $db_Connection->prepare($sql_Query);
		$query_Result = $statement->execute();
		
		if ($query_Result === null || $query_Result === false) {
			throw new \Exception("Bad SQL query result.");
		}
		// -------------------------------------------------------------|/\|---
		
		
		// Prepare result ----------------------------------------------|\/|---
		$affected_Rows = $statement->rowCount();
		// -------------------------------------------------------------|/\|---
		
		
		return $affected_Rows;
		
	}
	
	
	/**
	 * @see php_Util_DB_EDITOR::delete_Object()
	 */
	public function delete_Object($tablename, $conditions) {
		

		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		if ($conditions == null || $conditions == "") {
			throw new \Exception("Parameter conditions required.");
		}
		// --------------------------------------------------------------------|/\|---
		

		// Prepare SQL query --------------------------------------------------------|\/|---
		$sql_Query = null;
		try {
			$sql_Query = self::get_SQL__Delete($tablename, $conditions);
		} catch (\Exception $e) {
			throw new \Exception("Error in SQL query. ". $e->getMessage());
		}
		// --------------------------------------------------------------------------|/\|---
		
		// Execute query -----------------------------------------------|\/|---
		$db_Connection = $this->db_Connection->_connection_Link;
		$affected_Rows = $db_Connection->exec($sql_Query);
		// -------------------------------------------------------------|/\|---
		
		
		return $affected_Rows;
		
	}

	
}
